<%--
  Created by IntelliJ IDEA.
  User: qjqe@outlook.com
  Date: 2020/5/15
  Time: 9:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@page import="java.util.List"%>
<script>
    //重新刷新
    function reload() {
        location.reload();
    }
    function del(a){
        //$("#report").css("display","none");
        //$("#form").attr('style','display:inline')


        $.ajax(
            {
                url: "/course/delete",
                type: "post",
                datatype:"json",
                data:{"id":a},
                success: function (result) { //返回的结果自动放在resut里面了
                    if (result.status == "success") {//请求成功
                        alert("成功");
                        location.reload();
                    } else {
                        alert("失败");
                        location.reload();
                    }
                }
            });
    }
    //点击添加按钮后的插入表格
    $(function () {
        $(".add").click(function () {
            $("tbody").prepend( "<tr><td><input class='add0'></td><td><input class='add1'></td><th><button class=\"btn btn-default\" onclick=\"add()\">确定</button></th><th><button class=\"btn btn-default\" onclick=\"reload()\">取消</button></th></tr>");
        })


    })

    //插入功能里的提交数据
    function add() {
        var courseid=$(".add0").val();
        var coursename=$(".add1").val();

        $.ajax(
            {
                url: "/course/add",
                type: "post",
                datatype:"json",
                data:{"courseid":courseid,"coursename":coursename},
                success: function (result) { //返回的结果自动放在resut里面了

                    if (result.status == "success") {//请求成功
                        alert("成功");
                        location.reload();
                    } else {
                        alert("失败");
                        location.reload();
                    }
                }
            });
    }

</script>
<div id="report">
<h2 class="sub-header">课程列表</h2>
<div class="table-responsive">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>编号</th>
            <th>课程名字</th>
            <th><button class="btn btn-default btn-primary add">添加</button></th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items = "${requestScope.course }" var="course">
            <tr>
                <td >${course.courseid }</td>
                <td >${course.coursename }</td>
                <th><button class="btn btn-default delete" onclick="del(${course.courseid })">删除</button></th>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</div>
